package com.yuqiaotech.zcj.forum.action;

import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.config.ParentPackage;
import org.apache.struts2.config.Result;
import org.apache.struts2.config.Results;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.yuqiaotech.zcj.Constants;
import com.yuqiaotech.zcj.auth.model.User;
import com.yuqiaotech.zcj.dao.BaseDao;
import com.yuqiaotech.zcj.dao.PaginationSupport;
import com.yuqiaotech.zcj.forum.model.Category;
import com.yuqiaotech.zcj.forum.model.Forum;
import com.yuqiaotech.zcj.forum.model.Post;
import com.yuqiaotech.zcj.forum.model.Topic;
import com.yuqiaotech.zcj.service.GenericManager;

/**
 * 新增讨论区，讨论区类别，管理版主等。
 * 
 */
@ParentPackage(value = "normal")
@Results( {
		@Result(name = "login", value = "/login.jsp"),
		@Result(name = "categoryList", value = "/forum/categoryList.jsp"),
		@Result(name = "postEditForm", value = "/forum/postEditForm.jsp"),
		@Result(name = "forumEditForm", value = "/forum/forumEditForm.jsp"),
		@Result(name = "displayTopicPost", value = "/forum/displayTopicPost.jsp"),
		@Result(name = "listTopics", value = "/forum/listTopics.jsp") })
public class ForumadminAction {
	private GenericManager<Category, Long> categoryManager;
	private GenericManager<Forum, Long> forumManager;
	private GenericManager<Topic, Long> topicManager;
	private GenericManager<Post, Long> postManager;
	private Category category;
	private Forum forum;
	private Topic topic;
	private Post post;
	private List<Post> posts;
	private List<Forum> forums;
	private List<Category> categories;
	private List<Topic> topics;
	private long forumId;
	private long topicId;
	private BaseDao baseDao;
	private User user;
	private int number;
	private int numberCount;
	private PaginationSupport paginationSupport;
	private Integer pageNo = 1;
	private Integer pageSize = 3;
	private PaginationSupport topicsPagination;

	public String execute() {
		return listCategories();
	}

	public void category() {
		categories = categoryManager.findByHql("from Category");
	}

	public void user() {
		user = (User) ServletActionContext.getRequest().getSession()
				.getAttribute(Constants.USER_KEY);
	}

	public String listCategories() {
		category();
		return "login";
	}

	public String deleteCategory() {
		categoryManager.delete(category);
		return listCategories();
	}

	public String saveCategory() {
		categoryManager.save(category);
		return listCategories();
	}

	public String createForumForm() {
		category();
		return "forumEditForm";
	}

	public String saveForumForum() {
		forumManager.save(forum);
		return listCategories();
	}

	public String deleteForum() {
		forumManager.delete(forum);
		return listCategories();
	}

	@SuppressWarnings("unchecked")
	public String listTopics() {
		topics = baseDao.executeQuery("from Topic where forum_id=?",
					forumId);
		forum = forumManager.get(forumId);
		numberCount=topics.size();
		forum.setNumberCount(numberCount);
		 forumManager.update(forum);
		DetachedCriteria dc = DetachedCriteria.forClass(Topic.class);
		dc.add(Restrictions.eq("forum.id", forumId));
		topicsPagination = baseDao.paginateByCriteria(dc, pageNo, pageSize);
		return "listTopics";
	}

	public String displayTopic() {
		topic = topicManager.get(topicId);
		forum = topic.getForum();
		posts = postManager.findByHql("from Post where topic_id=?", topicId);
		return "displayTopicPost";
	}

	public String savetopicForum() {
		user();
		if (user != null) {
			topic.setCreate(user);
		}
		forum = forumManager.get(forumId);
		topic.setForum(forum);
		forum.setNumberCount(numberCount);
		topicManager.save(topic);
		return listTopics();
	}

	public String postTopicForm() {
		forum = forumManager.get(forumId);
		return "postEditForm";
	}

	public String reply() {
		topic = topicManager.get(topicId);
		forum = topic.getForum();
		Date now = new Date();
		post.setPostTime(now);
		post.setTopic(topic);
		user = (User) ServletActionContext.getRequest().getSession()
				.getAttribute(Constants.USER_KEY);
		if (user != null) {
			post.setCreator(user);
		}
		postManager.save(post);
		return displayTopic();
	}

	public String deletePost() {
		post = postManager.get(post.getId());
		topicId = post.getTopic().getId();
		postManager.delete(post);
		return displayTopic();
	}

	public String deleteTopic() {
		topic = topicManager.get(topicId);
		forumId = topic.getForum().getId();
		topicManager.delete(topic);
		return listTopics();
	}

	public String categoriesList() {
		category();
		return "categoryList";
	}

	public GenericManager<Category, Long> getCategoryManager() {
		return categoryManager;
	}

	public void setCategoryManager(
			GenericManager<Category, Long> categoryManager) {
		this.categoryManager = categoryManager;
	}

	public GenericManager<Forum, Long> getForumManager() {
		return forumManager;
	}

	public void setForumManager(GenericManager<Forum, Long> forumManager) {
		this.forumManager = forumManager;
	}

	public GenericManager<Topic, Long> getPostManager() {
		return topicManager;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public Forum getForum() {
		return forum;
	}

	public void setForum(Forum forum) {
		this.forum = forum;
	}

	public List<Forum> getForums() {
		return forums;
	}

	public void setForums(List<Forum> forums) {
		this.forums = forums;
	}

	public List<Category> getCategories() {
		return categories;
	}

	public void setCategories(List<Category> categories) {
		this.categories = categories;
	}

	public List<Topic> getTopics() {
		return topics;
	}

	public void setTopics(List<Topic> topics) {
		this.topics = topics;
	}

	public Topic getTopic() {
		return topic;
	}

	public void setTopic(Topic topic) {
		this.topic = topic;
	}

	public long getForumId() {
		return forumId;
	}

	public void setForumId(long forumId) {
		this.forumId = forumId;
	}

	public long getTopicId() {
		return topicId;
	}

	public void setTopicId(long topicId) {
		this.topicId = topicId;
	}

	public GenericManager<Topic, Long> getTopicManager() {
		return topicManager;
	}

	public void setTopicManager(GenericManager<Topic, Long> topicManager) {
		this.topicManager = topicManager;
	}

	public List<Post> getPosts() {
		return posts;
	}

	public void setPosts(List<Post> posts) {
		this.posts = posts;
	}

	public Post getPost() {
		return post;
	}

	public void setPost(Post post) {
		this.post = post;
	}

	public void setPostManager(GenericManager<Post, Long> postManager) {
		this.postManager = postManager;
	}

	public BaseDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}

	public User getUser() {
		return user;
	}

	public int getNumberCount() {
		return numberCount;
	}

	public void setNumberCount(int numberCount) {
		this.numberCount = numberCount;
	}

	public PaginationSupport getPaginationSupport() {
		return paginationSupport;
	}

	public void setPaginationSupport(PaginationSupport paginationSupport) {
		this.paginationSupport = paginationSupport;
	}

	public Integer getPageNo() {
		return pageNo;
	}

	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public PaginationSupport getTopicsPagination() {
		return topicsPagination;
	}

	public void setTopicsPagination(PaginationSupport topicsPagination) {
		this.topicsPagination = topicsPagination;
	}

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

}
